const {
  getBooksListSql,
  addBooksListSql,
  updateBooksListSql,
  deleteBooksListSql
} = require('../model/booksList')
const { useCreateID, useBooksData } = require('../utils/use')

/**
 * 获取所有书签列表
 * @path /api/books/list
 * @param {*} req { page, limit, books_id } 如果有书签id就获取书签列表
 * @param {*} res
 */
exports.getBooksList = async (req, res) => {
  try {
    const { page = 1, limit = 15, books_id } = req.query

    // 如果有书签id就获取书签列表
    const { result, total } = books_id
      ? await getBooksListSql(Number(page), Number(limit), books_id)
      : await getBooksListSql(Number(page), Number(limit))

    // 如果页数大于总页数
    if (page > Math.ceil(total / limit)) {
      return res.jsonSuccess({
        result: [],
        total
      })
    }

    // 处理书签列表格式
    const data = result.map((item) => {
      const { book_id, book_url, book_name, book_desc, book_ico, books_id, book_status } = item

      return {
        id: book_id,
        name: book_name,
        url: book_url,
        description: book_desc,
        ico_url: book_ico,
        parent_id: books_id,
        status: Number(book_status) === 1 ? 'public' : 'private'
      }
    })

    // 如果有书签id就获取书签列表
    const books = !books_id
      ? useBooksData(result)
      : {
          id: books_id,
          name: result[0].books_name,
          description: result[0].books_desc,
          status: Number(result[0]?.books_status) === 1 ? 'public' : 'private',
          children: data
        }

    // 如果有书签id就返回书签列表，如果没有就返回所有书签列表数据
    const result_data = books_id
      ? books
      : { data: books, page: Number(page), limit: Number(limit), total }

    res.jsonSuccess(result_data)
  } catch (error) {
    console.log('获取书签', error.message)
    res.jsonFail(400, '获取书签失败')
  }
}

/**
 * 添加书签列表
 * @path /api/books/list/add
 * @param {*} req
 * @param {*} res
 * @returns
 */
exports.addBooksList = async (req, res) => {
  try {
    const { books_name, books_desc = '' } = req.body

    // 参数校验
    if (!books_name) {
      return res.jsonFail(400, '参数错误')
    }

    const books_id = useCreateID()

    // 添加书签列表
    const result = await addBooksListSql(books_id, books_name, books_desc)

    // 判断是否添加成功
    if (!result) {
      return res.jsonFail(400, '添加失败')
    }

    res.jsonSuccess({
      id: books_id,
      name: books_name,
      description: books_desc
    })
  } catch (error) {
    res.jsonFail(400, error.message || '添加失败')
  }
}

/**
 * 修改书签列表
 * @path /api/books/list/update
 * @param {*} req
 * @param {*} res
 * @returns
 */
exports.updateBooksList = async (req, res) => {
  try {
    const { books_id, books_name, books_desc } = req.body

    // 参数校验
    if (!books_id || !books_name) {
      return res.jsonFail(400, '参数错误')
    }

    // 修改书签列表
    const result = await updateBooksListSql(books_id, books_name, books_desc)

    // 判断是否修改成功
    if (!result) {
      return res.jsonFail(400, '修改失败')
    }

    res.jsonSuccess({
      id: books_id,
      name: books_name,
      description: books_desc
    })
  } catch (error) {
    res.jsonFail(400, error.message || '修改失败')
  }
}

/**
 * 删除书签列表
 * @path /api/books/list/delete
 * @param {*} req
 * @param {*} res
 * @returns
 */
exports.deleteBooksList = async (req, res) => {
  try {
    const { books_id } = req.body

    // 参数校验
    if (!books_id) {
      return res.jsonFail(400, '参数错误')
    }

    // 删除书签列表
    const result = await deleteBooksListSql(books_id)

    // 判断是否删除成功
    if (!result) {
      return res.jsonFail(400, '删除失败')
    }

    res.jsonSuccess()
  } catch (error) {
    res.jsonFail(400, error.message || '删除失败')
  }
}
